//
//  userCommentsVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/29.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "UserCommentsVC.h"
#import "StarRateView.h"
#import "UserCommentsCell.h"
#import "RatingVC.h"
@interface UserCommentsVC ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)NSMutableArray * dataSource;

@property (nonatomic, strong)UIView * linecommentsHeaderView;

@property (nonatomic, strong)UILabel * lineNameLabel;

@property (nonatomic, strong)UILabel * scoreLabel;

@property (nonatomic, strong)UILabel * commentCountLabel;

@property (nonatomic, strong)StarRateView * starView;

@property (nonatomic, strong)UIButton * writeCommentBtn;

@property (nonatomic, strong) LineModel *lineModel;

@end

@implementation UserCommentsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setup];
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadDataSource];
}

- (UIButton *)writeCommentBtn {
    
    if (!_writeCommentBtn) {
        
        _writeCommentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_writeCommentBtn setBackgroundImage:[UIImage imageNamed:@"defaultBG"] forState:UIControlStateNormal];
        [_writeCommentBtn setTitle:MYLocalizedString(@"SITEPLAY_WRITE_COMMENT", nil) forState:UIControlStateNormal];
        
        CGFloat Y = kScreenHeight - 49;
        if (ISiPhoneX) {
            Y = kScreenHeight - 83;
        }
        _writeCommentBtn.frame = CGRectMake(0, Y,kScreenWidth, 49);
        [_writeCommentBtn addTarget:self action:@selector(submitComment) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _writeCommentBtn;
}

- (UIView *)linecommentsHeaderView {
    
    if (!_linecommentsHeaderView) {
        _linecommentsHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 112)];
        _linecommentsHeaderView.backgroundColor = [UIColor clearColor];
        _lineNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 8, kScreenWidth - 36, 40)];
        _lineNameLabel.numberOfLines = 2;
        _lineNameLabel.textColor = kTitleColor;//[UIColor whiteColor];
        _lineNameLabel.font = Font(17);
        _lineNameLabel.minimumScaleFactor = 0.9;
        _lineNameLabel.adjustsFontSizeToFitWidth = YES;
        //_lineNameLabel.text = @"法国不止巴黎，盘点法国那些精致小镇";
        [_linecommentsHeaderView addSubview:_lineNameLabel];
        
        if (self.type == 1) {
            
            _linecommentsHeaderView.frame = CGRectMake(0, 0, kScreenWidth, 84);
            _commentCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 53, 200, 15)];
            
        } else {
            
            _scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 53, 35, 21)];
            _scoreLabel.textColor = kThemeColor;
            _scoreLabel.font = Font(22);
            //_scoreLabel.text = @"4.0";
            [_linecommentsHeaderView addSubview:_scoreLabel];
            
            _starView = [[StarRateView alloc] initWithFrame:CGRectMake(55, 55.5, 120, 16)];
            [_starView setScore:4];
            
            [_linecommentsHeaderView addSubview:_starView];
            
            _commentCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 79, 200, 15)];
        }
        
        
        _commentCountLabel.textColor = kContentColor;// [UIColor whiteColor];
        _commentCountLabel.font = Font(12);
        //_commentCountLabel.text = @"401人评论";
        [_linecommentsHeaderView addSubview:_commentCountLabel];
    }
    
    
    return _linecommentsHeaderView;
}

- (void)loadDataSource {
    
    @weakify(self);
    [NetAPIManager getAllCommentsWithLineID:self.lineID isLine:self.type == 1 ? NO : YES Block:^(NSArray<Comment *> *commentArray, LineModel *linemodel, NSError *error) {
        @strongify(self);
        self.lineModel = linemodel;
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        [self.tableView.mj_header endRefreshing];
        self.tableView.tableHeaderView = self.linecommentsHeaderView;
        
        self.lineNameLabel.text = linemodel.linename;

        self.scoreLabel.text =[NSString stringWithFormat:@"%.1f ",ceil(linemodel.score.integerValue/10.0f)] ;
        
        [self.starView setScore:linemodel.score.floatValue / 10.0f];
        
        self.commentCountLabel.text = linemodel.questionTimes;
        self.commentCountLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"COMMENT_COUNT", nil),@(commentArray.count)];
        
        self.dataSource =  [NSMutableArray arrayWithArray:commentArray];
        [self.tableView reloadData];
        
        if (![linemodel.hasscore boolValue] && self.type != 1) {
            [self.view addSubview:self.writeCommentBtn];
        } else {
            
            [self.writeCommentBtn removeFromSuperview];
            CGRect tbRect = self.tableView.frame;
            tbRect.size.height = kScreenHeight - kBottomHeight - kNavigationBarHeight;
            self.tableView.frame = tbRect;
        }
    }];
}

- (void)setup {
    
   // self.customBarStyle = UIStatusBarStyleLightContent;
    self.barBGAlpha = 0;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,kNavigationBarHeight, kScreenWidth , kScreenHeight - 49 - kBottomHeight -kNavigationBarHeight ) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.estimatedRowHeight = 44;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:_tableView];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"UserCommentsCell" bundle:nil] forCellReuseIdentifier:@"UserCommentsCell"];
    
    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headerView"];
    self.title = MYLocalizedString(@"SITEPLAY_USER_COMMENT", nil);
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self loadDataSource];
    }];
    [self.tableView setThemeStyle:ThemeStyleWhite];
    
    self.view.backgroundColor =[UIColor whiteColor];// kThemeDarkColor;
    self.tableView.backgroundColor = [UIColor whiteColor];//kThemeDarkColor;
}

- (void)submitComment{
    
    RatingVC *rateVC = [RatingVC new];
    rateVC.isLine = YES;
    rateVC.relationid = self.lineModel.unionid;
    
    [rateVC show:self completeBlock:^(CGFloat score, NSString *comment) {
        [self.tableView.mj_header beginRefreshing];
    }];
}


#pragma mark  --UITableViewDelegate,UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCommentsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UserCommentsCell"];
    
    Comment * model = self.dataSource[indexPath.row];
    cell.type = self.type;
   // cell.style = WhiteStyle;
    
    [cell configDataWith:model];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    headerView.backgroundColor = [UIColor clearColor];
    headerView.contentView.backgroundColor = [UIColor clearColor];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 8;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
